package november.stack;

/**
 * @author huangxin
 * @date 2019/11/15-14:57
 */
public class ArrayStackDemo {
    public static void main(String[] args) {
        ArrayStack arrayStack = new ArrayStack(5);
        arrayStack.push(1);
        arrayStack.push(3);
        arrayStack.push(5);


        while (arrayStack.size() > 0) {
            System.out.println(arrayStack.pop());
        }
    }
}

class ArrayStack implements Stack<Integer> {
    int maxSize;
    int[] arr;
    int top;

    public ArrayStack(int maxSize) {
        this.maxSize = maxSize;
        arr = new int[maxSize];
        this.top = -1;
    }

    @Override
    public void push(Integer value) {
        if (isFull()) {
            System.out.println("栈已满");
            return;
        }
        top++;
        arr[top] = value;
    }


    @Override
    public Integer pop() {
        if (isEmpty()) {
            throw new RuntimeException("栈已为空");
        }
        int value = arr[top];
        top--;
        return value;
    }

    @Override
    public void show() {
        if (isEmpty()) {
            System.out.println("没有数据");
        }

        for (int i = top; i >= 0; i--) {
            System.out.printf("arr[%d] = %d\n", i, arr[i]);
        }
    }

    @Override
    public int size() {
        return top + 1;
    }

    public boolean isFull() {
        return top == maxSize - 1;
    }

    @Override
    public boolean isEmpty() {
        return top == -1;
    }
}
